<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

//use app\common\logic\User as CommonUser;

/**
 * 支付接口逻辑
 */
class Pay extends ApiBase
{
	/**
	 * 支付宝接口逻辑
	 */
	public function pcpay()
	{
		$res = [
			'subject'=>"购买1枚MDC",
			'body'=>"购买1枚MDC",
			'out_trade_no'=>7988945,
			'total_amount'=>0.01
		];
		// 引入支付核心文件
		import('zfbpay.AopSdk');
		import("zfbpay.pagepay.service.AlipayTradeService");
		import("zfbpay.pagepay.buildermodel.AlipayTradePagePayContentBuilder");
		// 获取支付宝配置参数
		//$config = C("alipay");
		$config=Config('alipay_config');
		//$config=$this->servicePay->driverAlipay->config();
		var_dump($config);exit;
		//商户订单号，商户网站订单系统中唯一订单号，必填
		$out_trade_no = $res["out_trade_no"];
		//订单名称，必填
		$subject = trim($res["subject"]);
		//付款金额，必填
		$total_amount = $res["total_amount"];
		//商品描述，可空
		$body = trim($res["body"]);
		//构造参数
		$payRequestBuilder = new \AlipayTradePagePayContentBuilder();
		$payRequestBuilder->setBody($body);
		$payRequestBuilder->setSubject($subject);
		$payRequestBuilder->setTotalAmount($total_amount);
		$payRequestBuilder->setOutTradeNo($out_trade_no);
		$aop = new \AlipayTradeService($config);
		/**
		* pagePay 电脑网站支付请求
		* @param $builder 业务参数，使用buildmodel中的对象生成。
		* @param $return_url 同步跳转地址，公网可以访问
		* @param $notify_url 异步通知地址，公网可以访问
		* @return $response 支付宝返回的信息
		*/
		$response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
		return $response;
	}
	/**
	 * 支付宝接口逻辑
	 */
    public function zfbpay($data)
    {
		$str=substr($data['order_sn'],0,2);
		if($str == 'gs'){
			$money=db('order_goods')->where('order_sn',$data['order_sn'])->value('money');
			$subject="购买商品";
		}elseif($str == 'xx'){
			$money=db('order')->where('order_sn',$data['order_sn'])->value('money');
			$subject="线下消费";
		}
		$order = [
                'subject'=>$subject,
                'out_trade_no'=>$data['order_sn'],
                'price'=>$money
            ];
        $url=$this->servicePay->driverAlipay->createAppPara($order);
		return $url;
    }

	/**
	 * 微信接口逻辑
	 */
    public function wxpay($data)
    {
		//$str=substr($data['order_sn'],0,2);
		$info=db('order_goods')->where('order_sn',$data['order_sn'])->find();
		$openid=db('user')->where('id',$info['user_id'])->value('openid');
		$order = [
                "body"=>'购买商品',
                "out_trade_no" => $data['order_sn'],
                "total_fee" => $info['money'],
                "spbill_create_ip" => get_client_ip(),
				"openid"=>$openid
            ];
        $url=$this->servicePay->driverWxpay->pay($order,'JSAPI');
		return $url;
    }
	/**
	 * 查询支付结果
	 */
    public function payStat($data)
    {
		$str=substr($data['order_sn'],0,2);
		if($str == 'gs') {
			$stat = db('order_goods')->where('order_sn', $data['order_sn'])->value('stat');
		}elseif($str == 'xx'){
			$stat=db('order')->where('order_sn',$data['order_sn'])->value('stat');
		}
		if($stat == 1){
			return $this->apiReturn();	
		}else{
			return ['code'=>1010003,'msg'=>'支付失败'];
		}
		
    }
	
}
